package biz.ezcom.spring.mvc.convertor.local;

import java.beans.PropertyEditorSupport;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateConvertor extends PropertyEditorSupport {
	private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Override
	public void setAsText(String text) {
		try {
			Date date = dateFormat.parse(text);
			setValue(date);
		} catch (ParseException e) {
			e.printStackTrace();
			setValue(new Date());
		} 
	}

	@Override
	public String getAsText() {
		Date date = (Date) getValue();
		return dateFormat.format(date);
	}
}
